Skip to content

Add context dependencies to Tool.from_schema() #2130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ferchobanana
Copy link

@ferchobanana ferchobanana commented Jul 4, 2025

This PR adds a "takes_ctx" optional boolean argument indicating whether the function accepts the context object as an argument. Solving #2122

@DouweM
Copy link
Contributor

DouweM commented Jul 4, 2025

@ferchobanana Thank you! Can you please run make format to fix the linting issue? In this case you could just remove the extra whitespace manually, but make format is a good habit to get into on this repo :)

And can you please add a test to tests/test_tools.py? (there are already some from_schema tests there)

And the docs also need updating:

pydantic-ai/docs/tools.md

Lines 472 to 473 in 2e12934

If you have a function that lacks appropriate documentation (i.e. poorly named, no type information, poor docstring, use of *args or **kwargs and suchlike) then you can still turn it into a tool that can be effectively used by the agent with the `Tool.from_schema` function. With this you provide the name, description and JSON schema for the function directly:

I don't think we need an example of takes_ctx, but now that the function supports more arguments, we should make Tool.from_schema a link to the API docs. The docs would also benefit from a heading:

### Custom tool schema

If you have a function that lacks appropriate documentation (i.e. poorly named, no type information, poor docstring, use of `*args` or `**kwargs` and suchlike) then you can still turn it into a tool that can be effectively used by the agent with the [`Tool.from_schema`][pydantic_ai.tools.Tool.from_schema] function. With this you provide the name, description and JSON schema for the function directly:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants